

let QQMapWX = require('../../lib/qqmap-wx-jssdk.js');
var qqmapsdk;
const app = getApp()
var amapFile = require('../../utils/amap-wx.130.js');//这个是高德地图的
Page({

  data: {
    lineList: null,
    city: ''
  },

  getUserLocation: function () {
    let vm = this;
    wx.getSetting({
      success: (res) => {
        console.log(JSON.stringify(res))
        // res.authSetting['scope.userLocation'] == undefined    表示 初始化进入该页面
        // res.authSetting['scope.userLocation'] == false    表示 非初始化进入该页面,且未授权
        // res.authSetting['scope.userLocation'] == true    表示 地理位置授权
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '请求授权当前位置',
            content: '需要获取您的地理位置，请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '拒绝授权',
                  icon: 'none',
                  duration: 1000
                })
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: '授权成功',
                        icon: 'success',
                        duration: 1000
                      })
                      //再次授权，调用wx.getLocation的API
                      vm.getLocation();
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none',
                        duration: 1000
                      })
                    }
                  }
                })
              }
            }
          })
        } else if (res.authSetting['scope.userLocation'] == undefined) {
          //调用wx.getLocation的API
          vm.getLocation();
        }
        else {
          //调用wx.getLocation的API
          vm.getLocation();
        }
      }
    })
  },

  getLocation: function () {
    let vm = this;
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(JSON.stringify(res))
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy;
        vm.getLocal(latitude, longitude)
      },
      fail: function (res) {
        console.log('fail' + JSON.stringify(res))
      }
    })
  },

  getLocal: function (latitude, longitude) {
    let vm = this;
    var qqmapsdk = new QQMapWX({
      key: '64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD'
    });
    qqmapsdk.reverseGeocoder({
      location: {
        latitude: latitude,
        longitude: longitude
      },
      success: function (res) {
        console.log("获取地址：" + res.result.ad_info.city);
        let province = res.result.ad_info.province
        let city = res.result.ad_info.city
        app.globalData.city = city;
        vm.data.city = city;
        console.log("city:" + city)

        vm.data.city = city;

      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        // console.log(res);
      }
    });
  },


  formSubmit: function (e) {
    var that = this;
    var saddress = e.detail.value.saddress;
    var eaddress = e.detail.value.eaddress;
    qqmapsdk = new QQMapWX({
      key: '64CBZ-OA5K4-KH3UV-DMCQX-2P4MJ-VJFQD'
    });


    qqmapsdk.geocoder({
      address: '广州市' + saddress,
      success: function (res) {
        var lat1 = (res.result.location.lat)
        var lng1 = (res.result.location.lng)
        console.log("lat:" + Number(lat1) + " lng:" + lng1)

        qqmapsdk.geocoder({
          address: '广州市' + eaddress,
          success: function (re) {
            var lat2 = (re.result.location.lat)
            var lng2 = (re.result.location.lng)

            console.log("lat1:" + lat1 + " lng1:" + lng1 + " lat2:" + lat2 + " lng:" + lng2)
            // var myAmapFun = new amapFile.AMapWX({ key: '7cf5924b8bfa7c82eb745b8a29258271' });//高德的api
            var myAmapFun = new amapFile.AMapWX({ key: 'b72465445640a96b7490b5a533e70871' });//高德的api
            app.globalData.lat1 = lat1;
            app.globalData.lng1 = lng1;
            app.globalData.lat2 = lat2;
            app.globalData.lng2 = lng2;
            var qishi = lng1 + ',' + lat1;
            var zd = lng2 + ',' + lat2;
            myAmapFun.getTransitRoute({//高德的路线规划功能。。。
              origin: qishi,
              destination:zd,
              city: '广州市',
              success: function (data) {
                console.log("看看高德地图的数据接口：",JSON.stringify(data));
                console.log(data.transits[0])
                if (data && data.transits) {
                  var transits = data.transits;
                  for (var i = 0; i < transits.length; i++) {
                    var segments = transits[i].segments;
                    transits[i].transport = [];
                    that.setData({
                      transits: transits
                    })
                    for (var j = 0; j < segments.length; j++) {
                      if (segments[j].bus && segments[j].bus.buslines && segments[j].bus.buslines[0] && segments[j].bus.buslines[0].name) {
                        var name = segments[j].bus.buslines[0].name
                        var tim='';
                      //  console.log("time:"+JSON.stringify(segments[j].bus.buslines[0],name))
                        if (j !== 0) {
                          name = '--' + name;
                        }
                        
                        transits[i].transport.push(name);
                      }
                    }
                  }
                }
                that.setData({
                  transits: transits
                });

              },
              fail: function (info) {

              }
            })

          },
          fail: function (res) {
            console.log(res);
          },
          complete: function (res) {
            console.log(res);
          }
        });
      },
      fail: function (res) {
        console.log(res);
      },
      complete: function (res) {
        console.log(res);
      }
    });
  },





  onLoad: function (options) {

    var that = this;




  },

  onReady: function () { },

  onShow: function () {
    var that = this

  },



  onHide: function () { },

  onUnload: function () { },

  onPullDownRefresh: function () { },

  onReachBottom: function () { },

  onShareAppMessage: function () { },



});